package com.dyh.quickdemo.demos.lb;

import com.google.common.collect.Range;
import org.assertj.core.util.Lists;
import org.junit.jupiter.api.Test;
import sun.security.provider.MD5;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/*
@Author=Joey Wheeler
@Date=2021/1/4 10:31
*/
public class WeightDemo {
    public static void main(String[] args) {
//        Range.closed(0,5);
//        System.out.println( Range.closed(0,5));
        f2();
    }

    public static void f2(){
        int[] weights = {1,2,2};

        Map<Integer,Integer> weightsMap= new HashMap<>();
        int idx = 0;
        for (int i=0;i<weights.length;i++) {
            int weight = weights[i];
            for (int j =0;j<weight;j++){
                weightsMap.put(idx++,i);
            }
        }
        Random r = new Random(System.currentTimeMillis());
        int res = r.nextInt(idx);//5 0~4

        System.out.println(weightsMap.get(res));



        System.out.println();
    }

    @Test
    public void test1(){
        int[] arr1 = {114,514};
        int[] arr2 = {114,1919};

        System.out.println(hash(arr1));
        System.out.println(hash(arr2));

    }

    public int hash(int[] arr){
        return  0xffff&( arr[0] & 0xff | arr[1]&0xff<<8);
    }

    /*&0xff 将高位0抹掉
    *混合低位的信息
    * */

    @Test
    public void test(){
        Object o1 = new Object();
        Object o2 = new Object();
        String s1 = "dawd";
        String s2 = "dawd";
        String s3 = "dawd123";

        Integer i1 = 1;
        Integer i2 = 1;
        Integer i3 = 2;

        System.out.println(o1.hashCode());
        System.out.println(o2.hashCode());

        System.out.println("==============");
        System.out.println(s1.hashCode());
        System.out.println(s2.hashCode());//s1 = s2
        System.out.println(s3.hashCode());

        System.out.println("==============");
        System.out.println(i1.hashCode());
        System.out.println(i2.hashCode());
        System.out.println(i3.hashCode());

        System.out.println();
    }

    /*一致性hash*/
    @Test
    public void test3(){
        /*按自然数顺序排序*/
        TreeMap<Integer,String> treeMap = new TreeMap<>();
        treeMap.put(2,"一");
        treeMap.put(3,"一");

        treeMap.put(4,"二");
        treeMap.put(5,"二");

        treeMap.put(0,"零");



        System.out.println(treeMap);//      {0=零, 2=一, 3=一, 4=二, 5=二}
        System.out.println(treeMap.tailMap(3)); //     {3=一, 4=二, 5=二}
        System.out.println(treeMap.lowerKey(4)); // 小于3 的最小key
       // System.out.println(treeMap.lastKey()); //

}
    @Test
    public void test4(){
        System.out.println("".length());
//        System.out.println(
//                new BigDecimal(2).multiply(BigDecimal.TEN).subtract(new BigDecimal(2))
//        );
//        List<String> list = Lists.newArrayList("11","22");
//        System.out.println(
//                list.contains(null)
//        );
        /*10*/
        String str = "XS";
        Pattern numPattern = Pattern.compile("\\d");
        Matcher m = numPattern.matcher(str);
        if (m.find( )) {
            System.out.println("Found value: " + m.start ());
        }
        /*java.lang.IllegalStateException: No match available
         */



    }

//    public static void f1(){
//        int[] weights = {1,2,2};
//
//        Arrays.stream(weights).flatMap(
//                e->
//        );
//    }


}
